<?php

namespace app\admin\model\service\order;

use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'service_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'traveltype_text',
        'choose_skill_type_text',
        'paytype_text',
        'to_shop_text',
        'starttime_text',
        'endtime_text',
        'actendtime_text',
        'is_complaint_text',
        'is_service_text',
        'is_pool_text',
        'is_settle_text',
        'status_text',
        'paytime_text',
        'accepttime_text',
        'gotime_text',
        'reachtime_text',
        'servicetime_text',
        'finishtime_text',
        'settletime_text'
    ];
    

    
    public function getTraveltypeList()
    {
        return ['0' => __('Traveltype 0'), '1' => __('Traveltype 1'), '2' => __('Traveltype 2')];
    }

    public function getChooseSkillTypeList()
    {
        return ['0' => __('Choose_skill_type 0'), '1' => __('Choose_skill_type 1')];
    }

    public function getPaytypeList()
    {
        return ['0' => __('Paytype 0'), '1' => __('Paytype 1'), '2' => __('Paytype 2'), '3' => __('Paytype 3'), '4' => __('Paytype 4')];
    }

    public function getToShopList()
    {
        return ['door' => __('To_shop door'), 'shop' => __('To_shop shop')];
    }

    public function getIsComplaintList()
    {
        return ['0' => __('Is_complaint 0'), '1' => __('Is_complaint 1')];
    }


    public function getIsServiceList()
    {
        return ['0' => __('Is_service 0'), '1' => __('Is_service 1'), '2' => __('Is_service 2'), '-1' => __('Is_service -1')];
    }

    public function getIsPoolList()
    {
        return ['0' => __('Is_pool 0'), '1' => __('Is_pool 1')];
    }

    public function getIsSettleList()
    {
        return ['0' => __('Is_settle 0'), '1' => __('Is_settle 1'), '2' => __('Is_settle 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('Status 6'), '7' => __('Status 7'), '-1' => __('Status -1')];
    }


    public function getTraveltypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['traveltype']) ? $data['traveltype'] : '');
        $list = $this->getTraveltypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getChooseSkillTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['choose_skill_type']) ? $data['choose_skill_type'] : '');
        $list = $this->getChooseSkillTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytype']) ? $data['paytype'] : '');
        $list = $this->getPaytypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getToShopTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['to_shop']) ? $data['to_shop'] : '');
        $list = $this->getToShopList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getActendtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['actendtime']) ? $data['actendtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getIsComplaintTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_complaint']) ? $data['is_complaint'] : '');
        $list = $this->getIsComplaintList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsServiceTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_service']) ? $data['is_service'] : '');
        $list = $this->getIsServiceList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsPoolTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_pool']) ? $data['is_pool'] : '');
        $list = $this->getIsPoolList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsSettleTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_settle']) ? $data['is_settle'] : '');
        $list = $this->getIsSettleList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAccepttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['accepttime']) ? $data['accepttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getGotimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gotime']) ? $data['gotime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getReachtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['reachtime']) ? $data['reachtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getServicetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['servicetime']) ? $data['servicetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getFinishtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['finishtime']) ? $data['finishtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSettletimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['settletime']) ? $data['settletime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setActendtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAccepttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setGotimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setReachtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setServicetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setFinishtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSettletimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getRank($type = 'goods_id',$timeType = '')
    {
        $field = 'id,goods_id,shop_id,skill_id,count(id) as num,sum(payprice) as total_price,city';
        $group = $type;
        if($timeType != 'all')
        {
            $list= db('service_order')->field($field)->whereTime('finishtime',$timeType)->group($group)->order('num desc')->limit(10)->select();
        }else{
            $list= db('service_order')->field($field)->group($group)->order('num desc')->limit(10)->select();
        }
        foreach ($list as &$value)
        {
            $value['goodsName'] = \app\admin\model\service\Goods::where('id',$value['goods_id'])->value('name');
            $value['skillName'] = $value['skill_id']?\app\admin\model\service\skill\Skill::where('id',$value['skill_id'])->value('name'):'平台';
            $value['shopName'] = $value['shop_id']?\app\admin\model\service\shop\Shop::where('id',$value['shop_id'])->value('name'):'平台';
        }
        return $list;
    }



}
